package com.example.springbootsfzy.controller.lishenao;

import com.example.springbootsfzy.domain.Members;
import com.example.springbootsfzy.service.lishenao.MembersService;
import com.example.springbootsfzy.util.R;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//会员信息表
//操作人员:李深奥
//操作步骤:增删改查
@RestController
@CrossOrigin
public class MembersController {
    @Autowired
    private MembersService membersService;
    //获取所有会员信息
    @GetMapping("/getmembersbyid")
    public R getAllMemberById( Long id) {
        return R.ok(membersService.selectByPrimaryKey(id));
    }

    //获取所有会员信息
    @GetMapping("/getmembers")
    public R getAllMember() {
        return R.ok(membersService.selectAll());
    }

    //增加会员信息
    @PostMapping("/addmembers")
    public R addMember(@RequestBody Members members) {
        int i = membersService.insert(members);
        if (i > 0) {
            return R.ok("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    //删除会员信息
    @DeleteMapping("/delmembers")
    public R deleteMember( Long id) {
        int i = membersService.deleteByPrimaryKey(id);
        if (i > 0) {
            return R.ok("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    //修改会员信息
    @PutMapping("/upmembers")
    public R updateMember(@RequestBody Members members) {
        int i = membersService.updateByPrimaryKey(members);
        if (i > 0) {
            return R.ok("修改成功");
        } else {
            return R.error("修改失败");
        }
    }



    //根据会员名字 反查会员id
    @GetMapping("/selectByUsername")
    public R selectByUsername(String username) {
        Members members = membersService.selectByUsername(username);
        if(members != null){
            return R.ok(members);
        }
        return R.fail("没有查到");
    }

}
